cc.Class({
    extends: cc.Component,

    properties: {
        playersLayout:cc.Layout,
        playerPrefab:cc.Prefab,

    },

    // use this for initialization
    onLoad: function () {
        // this.registerEvent()
    },

    registerEvent:function(){
        this.gameState = 1;                 // 加入游戏，未下注
        window.sock.node.on("11-3",(evt)=>{ //  BeginGame 游戏开始，发牌
             this.gameState = 2;            
        })

        window.sock.node.on("11-24",(evt)=>{ // EndRound 结束一局游戏
             this.gameState = 3;             
        })

        window.sock.node.on("11-24",(evt)=>{ // Result 游戏结算
             this.gameState = 1;             
        })

        window.sock.node.on("11-16",(evt)=>{        // 玩家牌的变化
            console.log("11-16",evt.detail)
            let playerList = window.room.playerList
            for(let i =0; i<playerList.length; ++i){
                if(playerList[i].player.uid == evt.detail.memberId){
                    playerList[i].player.cards = evt.detail.cardList
                    break;
                }
            }
            this.updatePlayersInfo()

        }),

        window.sock.node.on("11-14",(evt)=>{            //玩家数据发生变化，坐下，站起，都接受该消息
            // console.log("玩家加入：",evt.detail)
            console.log("玩家加入：",window.room.playerList)
            
            let isPlayerInGame = false
            let playerList = window.room.playerList

            if(evt.detail.player.uid == 0){             // uid为零为站起，删除玩家信息
                for(let i =0; i< playerList.length; ++i ){
                    if(playerList[i].index == evt.detail.index){
                        playerList.splice(i,1);
                        break;
                    }       
                }
            }else{                                      // uid非零坐下，加入玩家或更新玩家信息
                for(let i =0; i< playerList.length; ++i ){
                    if(playerList[i].player.uid == evt.detail.player.uid)
                        isPlayerInGame = true
                }
                if(isPlayerInGame == false)        // todo把玩家数据加入到room,创建并显示玩家
                    playerList.push(evt.detail);
            }
            
            this.updatePlayersInfo()
        });

        window.sock.node.on("11-6", (evt)=>{    // 玩家加入游戏,没有坐下
            // console.log(evt.detail)
            // console.log("hello world", window.user,window.room )
            this.updatePlayersInfo()
        });  
    },

    updatePlayersInfo:function(){
        let isSelfInGame = false
        let playerList = window.room.playerList
        this.playersLayout.node.removeAllChildren()
        for(let i =0; i< window.room.playerList.length; ++i){
            let isSelf = false;
            if(playerList[i].player.uid == window.user.uid ){
                isSelfInGame = true;
                isSelf = true;
            }
            this.addOnePlayer(playerList[i].player,i,this.gameState,isSelf)
        }
        if(isSelfInGame == false){      // 显示加号
            let playerItem = cc.instantiate(this.playerPrefab)
            playerItem.parent = this.playersLayout.node 
        }
    },

    addOnePlayer:function(playerInfo,index, gameState,isSelf){
        let playerItem = cc.instantiate(this.playerPrefab)
        playerItem.parent = this.playersLayout.node 
        playerItem.getComponent("TxSeat").registerEvent()
        playerItem.getComponent("TxSeat").displayActor()
        playerItem.getComponent("TxSeat").playerInfo = playerInfo 
        playerItem.getComponent("TxSeat").displayCard({"type":gameState, "isMe":isSelf})
    },

});
